package com.tianshuo.circledepend.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.core.PriorityOrdered;
import org.springframework.stereotype.Component;

/**
 * @ClassName : CustomBeanDefinitionRegistryPostProcessor
 * @Description : 模拟实现Spring内部的BeanFactoryPostProcessor测试看是否可以Spring内置的BeanDefinitionRegistryPostProcessor接口
 * 一起执行，
 * 测试发现通过@Component方式实例化的CustomBeanDefinitionRegistryPostProcessor，优先级还是低于内置的，原因CustomBeanDefinitionRegistryPostProcessor
 * 是被内置的扫描出来
 *
 * 内置的BeanDefinitionRegistryPostProcessor 的beanName为internalConfigurationAnnotationProcessor, 类名字ConfigurationClassPostProcessor
 * @Author : tianshuo
 * @Date: 2020-09-03 09:45999
 */
@Component
public class CustomBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor, PriorityOrdered {
   final Logger logger = LoggerFactory.getLogger(CustomBeanDefinitionRegistryPostProcessor.class);


    public CustomBeanDefinitionRegistryPostProcessor() {
        logger.info("CustomBeanDefinitionRegistryPostProcessor,实例化");
    }


    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        logger.info("自定义BeanDefinitionRegistryPostProcessor,registry: {}",registry);
        GenericBeanDefinition genericBeanDefinition = new GenericBeanDefinition();
        genericBeanDefinition.setBeanClass(NonOrderBeanDefinitionRegistryPostProcessor.class);
        registry.registerBeanDefinition("nonOrderBeanDefinitionRegistryPostProcessor",genericBeanDefinition);
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

        logger.info("自定义BeanDefinitionRegistryPostProcessor,beanFactory: {}",beanFactory);
    }

    @Override
    public int getOrder() {
        return 1;
    }
}

